package com.wind.config;

import com.wind.entiy.UserKind;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.index.Index;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 初始化数据
 *
 * @author wind
 * @date 2021/7/27 16:00
 */
@Component
public class Runner implements ApplicationRunner {

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public void run(ApplicationArguments args) {
        // 创建过期索引时，创建集合
        System.out.println("创建过期索引（开始）");
        // MongoDB每分钟执行一次过期检查，所以过期时间最小单位为分钟
        mongoTemplate.indexOps(UserKind.class).ensureIndex(new Index().on("createTime", Sort.Direction.DESC).expire(10, TimeUnit.SECONDS));
        System.out.println("创建过期索引（结束）");
    }
}
